(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var win = $.createWindow(L('win_ugd_form'));
	var ui = $.layout('ugd/form');
	var txtBrand = $.ui('text_field').create(L('hint_brand'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtStore = $.ui('text_field').create(L('hint_store'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtPrice = $.ui('text_field').create(L('hint_price'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	txtPrice.keyboardType = Ti.UI.KEYBOARD_DECIMAL_PAD;
	resizeTextFields([txtBrand, txtStore, txtPrice]);
	var selectedRetailer, selectedRetailerStore;

	var retailers = new Array();
	_.each(JSON.parse($.retailers()), function(e) {
		retailers.push($.model('name_value')(e.FullName, e.RetailerID));
	});
	var optionBrands = $.ui('dialog').create('Select a brand', retailers, -1, function(e) {
		txtBrand.val(e.name);
		selectedRetailer = e;
	});

	ui.viewBrand.add(txtBrand);
	ui.viewStore.add(txtStore);
	ui.viewPrice.add(txtPrice);

	$.on(['change', 'focus', 'blur'], txtBrand, function() {
		if (selectedRetailer && selectedRetailer.name != txtBrand.val()) {
			selectedRetailer = null;
			selectedRetailerStore = null;
		}
	});
	$.on(['change', 'focus', 'blur'], txtStore, function() {
		if (selectedRetailerStore && selectedRetailerStore.name != txtStore.val())
			selectedRetailerStore = null;
	});

	$.tap(ui.viewBrand, function(e) {
		if (e.source && e.source.id) {
			optionBrands.show();
		}
	});
	$.tap(ui.viewStore, function(e) {
		if (e.source && e.source.id && selectedRetailer) {
			var retailerStores = new Array();
			_.each(JSON.parse($.retailerStores()), function(e) {
				if (selectedRetailer) {
					if (e.RetailerID == selectedRetailer.value)
						retailerStores.push($.model('name_value')(e.Name, e.RetailerStoreID));
				} else
					retailerStores.push($.model('name_value')(e.Name, e.RetailerStoreID));
			});
			var optionStores = $.ui('dialog').create('Select a store', retailerStores, -1, function(e) {
				txtStore.val(e.name);
				selectedRetailerStore = e;
			});
			optionStores.show();
		}
	});

	$.tap([ui.btnSave, ui.btnSubmit], function(e) {
		var isSubmitted = e.source.isSubmitted == 'yes' ? true : false;
		if (isSubmitted) {
			win.submit(true);
		} else {
			$.askFor(L('ugd_save_confirm'), L('ugd_save_title'), ['YES', 'NO, post now'], 0, function(alsoPostIt) {
				win.submit(alsoPostIt);
			});
		}
	});

	win.submit = function(isSubmitted) {
		var successMessage = isSubmitted ? L('ugd_submit_success') : L('ugd_save_success');
		var retailerName = txtBrand.val();
		var retailerStoreName = txtStore.val();
		var price = txtPrice.val(), comment = ui.txtComment.value;
		price = $.isNull(price) ? '' : price.toString().replace(/,/g, ".");
		if (!$.isNull(price) && isNaN(price)) {
			$.a(L('ugd_price_invalid'));
			return;
		}
		if (retailerName && retailerName.length > 50) {
			$.a(L('ugd_brand_too_long'));
			return;
		}
		if (retailerStoreName && retailerStoreName.length > 200) {
			$.a(L('ugd_store_too_long'));
			return;
		}
		if (ui.txtComment && ui.txtComment.value && ui.txtComment.value.length > 50) {
			$.a(L('ugd_comment_too_long'));
			return;
		}
		if (isSubmitted && ($.isNull(retailerName) || $.isNull(retailerStoreName) || $.isNull(price))) {
			$.a(L('ugd_fields_required'), L('ugd_submit_title'));
			return;
		}

		var fullHeight = $.mainContentHeight(true);
		ui.view.height = fullHeight;
		$.repeat(function() {
			ui.view.scrollTo(0, 0);
		}, 10);
		txtBrand.keyboardToolbar.hide();
		$.mask();
		var retailerID = selectedRetailer ? selectedRetailer.value : '';
		var retailerStoreID = selectedRetailerStore ? selectedRetailerStore.value : '';
		if (win.edit) {
			$.webservice().editUGD(win.ugd.DesireID, retailerName, retailerStoreName, retailerID, retailerStoreID, price, comment, isSubmitted, function(e) {
				$.mask(false);
				var result = JSON.parse(e);
				if (result.Success) {
					$.a(successMessage, L('ugd_success_title'), null, function() {
						$.win('my_desires').hideView();
						//$.win('ugd/preview').close();
						$.win('ugd/form').close();
						$.win('my_desires').nav.reset();
						$.ui('tabgroup').setActiveTab(0);
						$.win('my_desires').display();
					});
				} else
					$.a(result.Error);
			});
		} else {
			var sku = win.missingBarcode;
			$.webservice().createUGD(retailerName, retailerStoreName, retailerID, retailerStoreID, price, comment, isSubmitted, sku, win.media, function(e) {
				$.mask(false);
				var result = JSON.parse(e);
				if (result.Success) {
					$.a(successMessage, L('ugd_success_title'), null, function() {
						$.win('my_desires').hideView();
						//$.win('ugd/preview').close();
						$.win('ugd/form').close();
						$.win('my_desires').nav.reset();
						$.ui('tabgroup').setActiveTab(0);
						$.win('my_desires').display();
					});
				} else
					$.a(result.Error);
			});
		}
	};

	win.add(ui.view);

	win.navbar = $.ui('navbar')(true, false, true, 'Image details');
	win.add(win.navbar);
	win.navbar.overwriteBack(function() {
		win.close();
	});

	$.on('focus', win, function() {
		$.ui('text_field').init([txtBrand, txtStore, txtPrice, ui.txtComment], ui.view, $.mainContentHeight(true));
	});
	$.on(['focus', 'change', 'blur'], ui.txtComment, function() {
		ui.hintComment.visible = ui.txtComment.value == '' ? true : false;
	});
	$.on('focus', ui.txtComment, function() {
		_.delay(function() {
			ui.view.height = $.isIphone5() ? 270 : 200;
		}, 200);
	});
	$.on('blur', ui.txtComment, function() {
		_.delay(function() {
			ui.view.height = $.mainContentHeight(true);
		}, 200);
	});

	win.update = function(ugd) {
		win.ugd = ugd;
		win.edit = true;
		$.emptyTextFields([txtBrand, txtStore, txtPrice, ui.txtComment]);
		txtBrand.val(ugd.RetailerName);
		txtStore.val(ugd.RetailerStoreName);
		txtPrice.val(ugd.Price);
		ui.txtComment.value = ugd.Comment;
		ui.hintComment.visible = $.isNull(ugd.Comment) ? true : false;
		win.open();
		if (!$.isNull(ugd.RetailerID))
			selectedRetailer = $.model('name_value')(ugd.RetailerName, ugd.RetailerID);
		if (!$.isNull(ugd.RetailerStoreID))
			selectedRetailerStore = $.model('name_value')(ugd.RetailerStoreName, ugd.RetailerStoreID);
	};
	win.create = function(media, barcode) {
		win.media = media;
		win.missingBarcode = barcode || null;
		win.edit = false;
		$.emptyTextFields([txtBrand, txtStore, txtPrice, ui.txtComment]);
		win.open();
		$.mask(false);
	};

	function resizeTextFields(fields) {
		_.each(fields, function(e) {
			e.top = 5;
			e.width = 260;
			e.height = 35;
			e.left = 0;
		});
	};

	module.exports = win;
})();
